<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Online status
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Online_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($uri, $num_rows = false)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 50;
		if ($uri['q'] != '')
		{
			$this->db->like('backlink', $uri['q']);
		}
		if ($uri['from'] != '')
		{
			$this->db->where(sprintf('date_added >= %1$s', $this->db->escape($uri['from'].' 00:00:00')));
		}
		if ($uri['to'] != '')
		{
			$this->db->where(sprintf('date_added <= %1$s', $this->db->escape($uri['to'].' 23:59:59')));
		}
		if ($num_rows)
		{
			$this->db->select('id');
			return $this->db->get('online')->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('*, IF ('.sprintf('date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
			$this->db->order_by('date_added DESC');
			$this->db->limit($uri['limit'], $uri['rowstart']);
			$query = $this->db->get('online');
			if ($query->num_rows() > 0)
			{
				return $query->result_array();
			}
			else
			{
				return false;
			}
		}
	}
	
	public function visit_month($month, $year)
	{
		$this->db->select('COUNT(id) AS visit, day');
		$this->db->where('month', $month);
		$this->db->where('year', $year);
		$this->db->group_by('day');
		$this->db->order_by('day ASC');
		$query = $this->db->get('online');
		$data = array();
		foreach ($query->result_array() as $val) {
			$data[(int)$val['day']] = $val['visit'];
		}
		return $data;
	}

	public function status_month($month, $year)
	{
		$this->db->select('COUNT(id) AS visit, trinhduyet');
		$this->db->where('month', $month);
		$this->db->where('year', $year);		
		$this->db->group_by('trinhduyet');
		$this->db->order_by('trinhduyet ASC');
		$query = $this->db->get('online');
		return $query->result_array();
	}

	public function total_month($month, $year)
	{
		$this->db->select('id');
		$this->db->where('month', $month);
		$this->db->where('year', $year);
		$query = $this->db->get('online');
		return $query->num_rows();
	}

	public function visit_year($year)
	{
		$this->db->select('COUNT(id) AS visit, month');
		$this->db->where('year', $year);		
		$this->db->group_by('month');
		$this->db->order_by('month ASC');
		$query = $this->db->get('online');
		$data = array();
		foreach ($query->result_array() as $val) {
			$data[(int)$val['month']] = $val['visit'];
		}
		return $data;
	}

	public function status_year($year)
	{
		$this->db->select('COUNT(id) AS visit, trinhduyet');
		$this->db->where('year', $year);		
		$this->db->group_by('trinhduyet');
		$this->db->order_by('trinhduyet ASC');
		$query = $this->db->get('online');
		return $query->result_array();
	}

	public function total_year($year)
	{
		$this->db->select('id');
		$this->db->where('year', $year);
		$query = $this->db->get('online');
		return $query->num_rows();
	}

	public function visit_city($month, $year)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$startDay = $year.'-'.$month.'-01 00:00:00';
		$endDay = $year.'-'.$month.'-31 23:59:59';
		$this->db->select('COUNT(id) AS visit, region AS city');
		$this->db->where(sprintf('date_added >= %1$s AND date_added <= %2$s', $this->db->escape($startDay), $this->db->escape($endDay)));
		$this->db->group_by('region');
		$this->db->order_by('region ASC');
		$query = $this->db->get('online');
		return $query->result_array();
	}

	public function visit_country($month, $year)
	{
		$startDay = $year.'-'.$month.'-01 00:00:00';
		$endDay = $year.'-'.$month.'-31 23:59:59';
		$this->db->select('COUNT(id) AS visit, country');
		$this->db->where(sprintf('date_added >= %1$s AND date_added <= %2$s', $this->db->escape($startDay), $this->db->escape($endDay)));
		$this->db->group_by('country');
		$this->db->order_by('country ASC');
		$query = $this->db->get('online');
		return $query->result_array();
	}

	public function visit_backlink($month, $year)
	{
		$this->db->select('COUNT(id) AS visit, backlink');
		$this->db->where('month', $month);
		$this->db->where('year', $year);
		$this->db->group_by('backlink');
		$this->db->order_by('backlink ASC');
		$query = $this->db->get('online');
		return $query->result_array();
	}

}

/* End of file online_model.php */
/* Location: ./application/models/online_model.php */